package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //1. 保存套餐的基本信息 setmeal
        this.save(setmealDto);

        //2. 保存套餐的关联菜品数据 setmeal_dish --------> 批量
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }


    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //1. 查询该批次套餐的状态是否有启售的, 如果存在, 提示错误信息
        //select count(*) from setmeal where id in (1415580119015145474,1464239560702582785) and status = 1;
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(lambdaQueryWrapper);
        if(count > 0){
            throw new CustomException("当前删除的套餐中, 有正在启售的套餐,不能直接删除!");
        }

        //2. 先删除套餐的基本信息
        this.removeByIds(ids);


        //3. 再删除套餐的关联菜品信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }





    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //更新套餐的基本信息
        this.updateById(setmealDto);

        //更新套餐关联的菜品列表数据
        //先删除原来的菜品关系
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //再添加新的菜品关系
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)) {
            setmealDishes.stream().forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDto.getId());
            });
        }
        setmealDishService.saveBatch(setmealDishes);
    }
}
